#include <tuple>
#include <vector>
#include "connect.hpp"
#include "result.hpp"
#include "execute.hpp"

using namespace easymysql;

struct user
{
    user(int i, const std::string& n, bool a) : id(i), name(n), active(a) {}

    int id;
    std::string name;
    bool active;
};


void test_select(const connection& conn)
{
    result res = executer(conn).execute("select * from user;");

    using row_type = std::tuple<int, std::string, bool>;

    std::vector<std::string> fields = res.get_field_names();

#define FIRST
#if defined (FIRST)

    row_type row;

    while (res.get<row_type>(&row))
    {
        std::cout << fields[0] << ": " << std::get<0>(row) << ", " 
                  << fields[1] << ": " << std::get<1>(row) << ", " 
                  << fields[2] << ": " << std::get<2>(row) << std::endl;
    }

#else
    optional<row_type> opt_row;

    while (!( opt_row = res.get<row_type>() ))
    {
        std::cout << fields[0] << ": " << std::get<0>(*opt_row) << ", " 
                  << fields[1] << ": " << std::get<1>(*opt_row) << ", " 
                  << fields[2] << ": " << std::get<2>(*opt_row) << std::endl;
    }
#endif
}

void test_insert(const connection& conn)
{
    user user(3, "nana", true);
    executer(conn).execute("insert into user (id, name, active) values ({}, '{}', {});", user.id, user.name, user.active);
}

int main()
{
    connection conn("localhost", "user", "passwd", "dbname", 3306);

    // test_select(conn);

    test_insert(conn);

    return 0;
}
